package org.netbeans.modules.scss.options;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileUtil;
import org.openide.util.NbBundle;

final class ScssPanel extends javax.swing.JPanel {

    private final ScssOptionsPanelController controller;

    ScssPanel(ScssOptionsPanelController controller) {
	this.controller = controller;
	initComponents();
	// TODO listen to changes in form fields and call controller.changed()
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cbCompileOnSave = new javax.swing.JCheckBox();
        cbOutputStyle = new javax.swing.JComboBox();
        cbLineComments = new javax.swing.JCheckBox();
        cbDebugInfo = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        lblHamlVersion = new javax.swing.JLabel();
        btnBrowseSassPath = new javax.swing.JButton();
        lblSassPath = new javax.swing.JLabel();
        txtSassPath = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(cbCompileOnSave, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.cbCompileOnSave.text")); // NOI18N
        cbCompileOnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCompileOnSaveActionPerformed(evt);
            }
        });

        cbOutputStyle.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "compact", "compressed", "expanded", "nested" }));

        org.openide.awt.Mnemonics.setLocalizedText(cbLineComments, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.cbLineComments.text")); // NOI18N
        cbLineComments.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbLineCommentsActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cbDebugInfo, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.cbDebugInfo.text")); // NOI18N
        cbDebugInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDebugInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbOutputStyle, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbLineComments)
                        .addGap(52, 52, 52)
                        .addComponent(cbDebugInfo))
                    .addComponent(cbCompileOnSave))
                .addContainerGap(194, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbOutputStyle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbLineComments)
                    .addComponent(cbDebugInfo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbCompileOnSave)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        org.openide.awt.Mnemonics.setLocalizedText(lblHamlVersion, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.lblHamlVersion.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnBrowseSassPath, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.btnBrowseSassPath.text")); // NOI18N
        btnBrowseSassPath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseSassPathActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(lblSassPath, org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.lblSassPath.text")); // NOI18N

        txtSassPath.setText(org.openide.util.NbBundle.getMessage(ScssPanel.class, "ScssPanel.txtSassPath.text")); // NOI18N
        txtSassPath.setMaximumSize(new java.awt.Dimension(6, 20));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(lblSassPath)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(lblHamlVersion, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                        .addGap(40, 40, 40))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtSassPath, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnBrowseSassPath)
                        .addGap(60, 60, 60))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSassPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBrowseSassPath)
                    .addComponent(lblSassPath))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblHamlVersion, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(72, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cbLineCommentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbLineCommentsActionPerformed
	// TODO add your handling code here:
}//GEN-LAST:event_cbLineCommentsActionPerformed

    private void cbCompileOnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCompileOnSaveActionPerformed
	// TODO add your handling code here:
}//GEN-LAST:event_cbCompileOnSaveActionPerformed

    private void btnBrowseSassPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseSassPathActionPerformed
	JFileChooser chooser = new JFileChooser(txtSassPath.getText());
	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	int r = chooser.showDialog(
		SwingUtilities.getWindowAncestor(this),
		org.openide.util.NbBundle.getMessage(ScssPanel.class, "LBL_Select"));
	if (r == JFileChooser.APPROVE_OPTION) {
	    File file = chooser.getSelectedFile();
	    if (!file.isDirectory() || file.getPath().contains(" ")) {
		DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
			NbBundle.getMessage(ScssPanel.class, "ERROR_NotFile", file),
			NotifyDescriptor.Message.WARNING_MESSAGE));
		return;
	    }
	    file = FileUtil.normalizeFile(file);
	    txtSassPath.setText(file.getAbsoluteFile().toString());
	    displayVersion();
	}
}//GEN-LAST:event_btnBrowseSassPathActionPerformed

    private void cbDebugInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDebugInfoActionPerformed
	// TODO add your handling code here:
    }//GEN-LAST:event_cbDebugInfoActionPerformed

    void load() {
	txtSassPath.setText(ScssSettings.getDefault().getSassPath());
	displayVersion();
	cbCompileOnSave.setSelected(ScssSettings.getDefault().isCompileOnSave());
	cbLineComments.setSelected(ScssSettings.getDefault().isLineCommentsEnabled());
	cbDebugInfo.setSelected(ScssSettings.getDefault().isDebugInfoEnabled());
	cbOutputStyle.setSelectedIndex(ScssSettings.getDefault().getOutputStyle().id);

    }

    void store() {
	ScssSettings.getDefault().setHamlPath(txtSassPath.getText());
	ScssSettings.getDefault().setCompileOnSave(cbCompileOnSave.isSelected());
	ScssSettings.getDefault().setLineCommentsEnabled(cbLineComments.isSelected());
	ScssSettings.getDefault().setDebugInfoEnabled(cbDebugInfo.isSelected());
	ScssSettings.getDefault().setOutputStyle(cbOutputStyle.getSelectedIndex());
    }

    boolean valid() {
	// TODO check whether form is consistent and complete
	return true;
    }

    void displayVersion() {
	String version = null;
	File file = new File(txtSassPath.getText());
	if (file.exists() && new File(file, "lib" + File.separator + "sass" + File.separator + "engine.rb").exists()) { //NOI18N
	    version = ScssSettings.getHamlVersion(file);
	}

	if (version != null) {
	    lblHamlVersion.setText(NbBundle.getMessage(ScssPanel.class, "LBL_HamlVersion", version));
	} else {
	    lblHamlVersion.setText(NbBundle.getMessage(ScssPanel.class, "ERR_NoValidInstallation"));
	}
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowseSassPath;
    private javax.swing.JCheckBox cbCompileOnSave;
    private javax.swing.JCheckBox cbDebugInfo;
    private javax.swing.JCheckBox cbLineComments;
    private javax.swing.JComboBox cbOutputStyle;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblHamlVersion;
    private javax.swing.JLabel lblSassPath;
    private javax.swing.JTextField txtSassPath;
    // End of variables declaration//GEN-END:variables
}
